<?php
	/*
	Info: Creates a story by inserting the first line & title of the story
	Author: Pritam Pebam
	Version: 1.0
	Contact: ptamzz@gmail.com; @ptamzz
	URL: http://12bubbles.com
	*/
	
	session_start();
	require_once('../inc/fnc.php');
	require_once('../inc/conn.php');

	if(is_logged_in()) {
		// Initializing variables
		$line = mysql_prep(htmlentities($_POST['line']));
		$title = mysql_prep(htmlentities($_POST['title']));
		
		$userId = $_SESSION['userId'];

		$time = time();
		$sid = getnewstoryid();
		
		$query = "INSERT INTO `story` (`storyid`, `storyname`, `crtdbyid`, `delflag`, `crtdtime`) 
					VALUES ('{$sid}','{$title}', '{$userId}', 'N', NOW())";
					  
		$result = mysql_query( $query, $connection);

	
		//Getting the id number of the last inserted row
		$query = "INSERT INTO `entryline` (`storyid`, `storylineid`,`entrylineid`, `entryline`, `crtdbyid`, `delflag`, `crtdtime` ) 
				VALUES ('{$sid}',1, 1, '{$line}', '{$userId}', 'N',NOW())";
		$result = mysql_query( $query, $connection);

		$query = "INSERT INTO `storyline` (`storyid`, `storylineid`, `storyline`,`totalvote`, `crtdbyid`, `delflag`, `crtdtime` ) 
				VALUES ('{$sid}', 1, '{$line}',0, '{$userId}', 'N',NOW())";
		$result = mysql_query( $query, $connection);

		addtonotification($sid,1,0,2);
		$html = getLine(1, $line, $userId);						// getLine(Serial Number, line Body, User id)
		$html .= getEntryForm($storyId, 1);						//display post form - getEntryForm(Story Id, line Id)

		echo $sid . "/" . getStoryTitle($sid);
	} else {
		unset($_SESSION['indicator']);
		unset($_SESSION['redirecturl']);
		$_SESSION['redirecturl'] = $_SERVER['HTTP_REFERER'];
		$url = explode("?",$_SESSION['redirecturl']);
		$_SESSION['redirecturl'] = $url[0];
		$_SESSION['nsline'] = $line;
		$_SESSION['nstitle'] = $title;
		$_SESSION['indicator'] = '01';
		echo 'visitor';
	}
?>
